<template>
  <a-modal
    :title="isEdit ? '修改工序' : '添加工序'"
    :visible="show"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :maskClosable="false"
    width="30%"
    class="procedure-modal"
  >
    <div class="title">
      <a-form-model
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 14 }"
        :model="reviseForm"
        :rules="reviseformRules"
        ref="reviseForm"
      >
        <a-form-model-item label="工序名称" required prop="StdOperationName">
          <a-input v-model="reviseForm.StdOperationName" />
        </a-form-model-item>
        <a-form-model-item label="工序描述">
          <a-textarea
            style="resize: none"
            v-model="reviseForm.StdOperationDesc"
          />
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>
import { insStdOperation, updStdOperation } from "@/api/StdOperation/index";
export default {
  props: ["show", "isEdit", "reviseForm"],
  data() {
    return {
      confirmLoading: false, //moadl表格弹出loading
      reviseformRules: {
        StdOperationName: [
          {
            required: true,
            message: "请填写工序名称",
            trigger: "blur",
            help: "",
          },
        ],
      }, //添加工序名称校验
    };
  },
  methods: {
    //关闭弹窗
    handleCancel() {
      this.$refs.reviseForm.clearValidate();
      this.$emit("update:show", false);
    },
    //关闭弹窗后的处理
    breakclose() {
      this.$refs.reviseForm.clearValidate();
      this.$emit("break");
    },
    //确认添加/修改
    handleOk() {
      this.$refs.reviseForm.validate(async (valid) => {
        if (valid) {
          this.confirmLoading = true;
          //true为修改 false为新增
          if (this.isEdit) {
            const {
              StdOperationID,
              StdOperationName,
              StdOperationDesc,
              Status,
            } = this.reviseForm;
            const obj = {
              StdOperationID,
              StdOperationName,
              StdOperationDesc,
              Status,
            };
            const res = await updStdOperation([obj]);
            res.Data == ""
              ? this.$message.info("修改成功")
              : this.$message.info(res.Data);

            this.handleCancel();
            this.breakclose();
            this.confirmLoading = false;
          } else {
            const res = await insStdOperation([this.reviseForm]);
            res.Data == ""
              ? this.$message.info("添加成功")
              : this.$message.info(res.Data);

            this.handleCancel();
            this.breakclose();
            this.confirmLoading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
